package com.sec.android.app.myfiles.module.shortcut;

import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.DragAndDropMgr;
import com.sec.android.app.myfiles.fragment.home.HomeFragment;
import com.sec.android.app.myfiles.fragment.home.HomeFragmentItem;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.ModuleLoader;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.file.LocalFileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.DrmUtils;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.ResizeListView;

public class ShortcutHomeItem extends HomeFragmentItem implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, LoaderManager.LoaderCallbacks<Cursor> {

    private View mShortcutLayout;
    private ResizeListView mListView;
    private ShortcutAdapterImp mAdapterImp;
    private ShortcutLoadListener mLoadListener;
    private DragAndDropMgr mDragDrop;

    public ShortcutHomeItem(Context context, HomeFragment fragment) {
        super(context, fragment);
    }

    @Override
    protected void setListItem() {
        mAdapterImp = new ShortcutAdapterImp(mContext, mFragment.getNavigationInfo(), R.layout.home_shortcut_list_item, null, 0);
        mDragDrop = new DragAndDropMgr(mFragment, FileRecord.StorageType.Shortcut);

        try {
            mFragment.getLoaderManager().initLoader(R.id.shortcut_list, null, this);
        } catch (IllegalStateException e) {
            Log.e(this, "IllegalStateException:" + e.toString());
        }
    }

    @Override
    protected void _onResume() {
        reload();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (UiUtils.isValidClick(view.getId())) {
            ShortcutFileRecord shortcutRecord = (ShortcutFileRecord) mAdapterImp.getFileRecord(position);
            if (shortcutRecord != null) {
                String path = shortcutRecord.getFullPath();
                FileRecord record;
                if (DrmUtils.isDRMFile(mContext, path)) {
                    record = new LocalFileRecord(path, mContext);
                } else {
                    record = ModuleLoader.getInstance().createFileRecord(FileRecord.StorageType.Local, path);
                }

                NavigationManager navigationMgr = NavigationManager.getInstance(mFragment.getProcessId());
                NavigationInfo curInfo = navigationMgr.getCurInfo();
                if (curInfo != null && curInfo.isSelectOpDestination()) {
                    SamsungAnalyticsLog.sendLog(mFragment.getProcessId(), SamsungAnalyticsLog.Event.SHORTCUT, "Shortcut folder", null);
                } else {
                    SamsungAnalyticsLog.sendLog(mFragment.getProcessId(), SamsungAnalyticsLog.Event.SHORTCUT, shortcutRecord.isDirectory() ? "Folder" : "File", null);
                }

                if (FileUtils.isValidFile(mContext, record)) {
                    MyFilesFacade.executeRecord(mFragment.getProcessId(), mFragment.getActivity(), record);
                }
            }
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
        return new CursorLoader(mContext, mAdapterImp.getUri(), null, mAdapterImp.getSelection(), null, mAdapterImp.getOrderBy());
    }

    private void showLayout(boolean bShowLayout) {
        if (bShowLayout) {
            if (mShortcutLayout != null && mListView != null) {
                setViewVisibility(mShortcutLayout, View.VISIBLE);
                return;
            }
            ViewStub stub = (ViewStub) mRootView.findViewById(R.id.shortcut_stub);

            if (stub != null) {
                mShortcutLayout = stub.inflate();
                mListView = (ResizeListView) mShortcutLayout.findViewById(R.id.shortcut_list);
                mListView.setAdapter(mAdapterImp);
                mListView.setOnItemClickListener(this);
                mListView.setOnItemLongClickListener(this);
                mListView.setFocusableInTouchMode(false);
                mDragDrop.attachDragListenerToView(mListView);
                TextView subHeader = (TextView) mShortcutLayout.findViewById(R.id.home_shortcut_header);
                setContentDescription(subHeader, R.string.sheader_shortcuts);
            }
        } else {
            setViewVisibility(mShortcutLayout, View.GONE);
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (data != null) {
            boolean bShowLayout = false;
            if (data.getCount() > 0) {
                bShowLayout = true;
            }

            showLayout(bShowLayout);

            mAdapterImp.swapCursor(data);
            if (mLoadListener != null) {
                mLoadListener.shortcutLoadFinished();
            }
            setItemEnabled(mIsEnabled);

        }
    }

    public int getTotalFileCount() {
        int ret = 0;
        if (mListView != null) {
            ret = mListView.getCount();
        }
        return ret;
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
    }

    @Override
    protected void _onDestroy() {
    }

    @Override
    protected void _onPause() {
    }

    @Override
    protected void setItemEnabled(boolean enabled) {
        mIsEnabled = enabled;
        setChildItemEnabled(mListView, enabled);
    }

    @Override
    protected void setChildItemEnabled(View view, boolean bEnable) {
        super.setChildItemEnabled(view, bEnable);
        if (mAdapterImp.getCursor() != null && !mAdapterImp.getCursor().isClosed()) {
            mAdapterImp.setEnabled(bEnable);
            if (mListView != null) {
                mListView.setEnabled(bEnable);
                mListView.invalidateViews();
            }
        }
    }

    public void reload() {
        if (mFragment.isAdded()) {
            mFragment.getLoaderManager().restartLoader(2, null, this);
        }
    }

    public void setShortcutLoadListener(ShortcutLoadListener l) {
        mLoadListener = l;
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (UiUtils.isValidClick(view.getId()) && NavigationManager.isPossibleToActionMode()) {
            ShortcutFileRecord shortcutRecord = (ShortcutFileRecord) mAdapterImp.getFileRecord(position);
            int processId = mFragment.getProcessId();
            SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.LONG_PRESS, shortcutRecord.isDirectory() ? "Folder" : "File", null);
            MyFilesFacade.manageShortcut(processId, position);
        } else {
            Toast.makeText(mContext, R.string.another_process_is_already_in_progress, Toast.LENGTH_LONG).show();
        }
        return true;
    }

    public interface ShortcutLoadListener {
        void shortcutLoadFinished();
    }

    public AbsListView getShortcutListView() {
        return mListView;
    }
}
